﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class AddScheme : CustomForm
    {
        private Form1 _parent;

        public AddScheme(Form1 parent)
        {
            InitializeComponent();
            _parent = parent;

            RefreshSchemeDataGridView();
            SetCurrentScheme(null);
        }

        private void RefreshSchemeDataGridView()
        {
            SchemesDataGridView.Rows.Clear();
            foreach(var scheme in _parent.Schemes)
            {
                int index = SchemesDataGridView.Rows.Add();
                SchemesDataGridView.Rows[index].Cells["SchemeName"].Value = scheme.SchemeName;
            }
        }

        private void SaveSchemeButton_Click(object sender, EventArgs e)
        {
            //if(_parent.Schemes.Find(r => r.SchemeName == TaskNameTextBox.Text) != null)
            //{
            //    MessageBox.Show(string.Format("已经存在名为“{0}”的方案", TaskNameTextBox.Text));
            //    return;
            //}
            var row = SchemesDataGridView.SelectedRows[0];
            int index = SchemesDataGridView.Rows.IndexOf(row);
            RenderScheme scheme = _parent.Schemes[index];
            scheme.SchemeName = TaskNameTextBox.Text;
            scheme.SourceTempalteName = SourceTemplateNameTextBox.Text;
            scheme.SourceConfigName = SourceConfigNameTextBox.Text;
            scheme.TargetTempalteName = TargetTemplateNameTextBox.Text;
            scheme.TargetConfigName = TargetConfigNameTextBox.Text;

            HandleSelectScheme(scheme);

            MessageBox.Show("保存成功！");
        }

        private void HandleAddScheme(RenderScheme scheme)
        {
            _parent.Schemes.Add(scheme);

            int index = SchemesDataGridView.Rows.Add();
            SchemesDataGridView.Rows[index].Cells["SchemeName"].Value = scheme.SchemeName;

            HandleSelectScheme(scheme);
        }

        private void HandleSelectScheme(RenderScheme scheme)
        {
            SetCurrentScheme(scheme);

            int index = _parent.Schemes.IndexOf(scheme);
            DataGridViewRow row = SchemesDataGridView.Rows[index];
            SchemesDataGridView.CurrentCell = row.Cells[0];
            row.Cells["SchemeName"].Value = scheme.SchemeName;
        }

        private void SelectSourceTemplateButton_Click(object sender, EventArgs e)
        {
            ShowFileDialog("请选择源文档模板文件", "所有文件(*doc*;*xls*;*ppt*)|*.doc*;*.xls*;*.ppt*", false, control =>
            {
                SourceTemplateNameTextBox.Text = control.FileName;
            });
        }

        private void SelectSourceConfigButton_Click(object sender, EventArgs e)
        {
            ShowFileDialog("请选择源文档模板配置文件", "所有文件(*json*)|*.json*", false, control =>
            {
                SourceConfigNameTextBox.Text = control.FileName;
            });
        }

        private void SelectTargetTemplateButton_Click(object sender, EventArgs e)
        {
            ShowFileDialog("请选择目标文档模板文件", "所有文件(*doc*;*xls*;*ppt*)|*.doc*;*.xls*;*.ppt*", false, control =>
            {
                TargetTemplateNameTextBox.Text = control.FileName;
            });
        }

        private void SelectTargetConfigButton_Click(object sender, EventArgs e)
        {
            ShowFileDialog("请选择目标文档模板配置文件", "所有文件(*json*)|*.json*", false, control =>
            {
                TargetConfigNameTextBox.Text = control.FileName;
            });
        }

        private void AddSchemeButton_Click(object sender, EventArgs e)
        {
            RenderScheme renderScheme = new RenderScheme();
            renderScheme.SchemeName = "未命名方案";
            HandleAddScheme(renderScheme);
        }

        private void SetCurrentScheme(RenderScheme scheme)
        {
            if(scheme == null)
            {
                TaskNameTextBox.Text = "";
                SourceTemplateNameTextBox.Text = "";
                SourceConfigNameTextBox.Text = "";
                TargetTemplateNameTextBox.Text = "";
                TargetConfigNameTextBox.Text = "";

                AddSchemePanel.Enabled = false;
            }
            else
            {
                TaskNameTextBox.Text = scheme.SchemeName;
                SourceTemplateNameTextBox.Text = scheme.SourceTempalteName;
                SourceConfigNameTextBox.Text = scheme.SourceConfigName;
                TargetTemplateNameTextBox.Text = scheme.TargetTempalteName;
                TargetConfigNameTextBox.Text = scheme.TargetConfigName;

                AddSchemePanel.Enabled = true;
            }
        }

        private void SchemesDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var scheme = _parent.Schemes[e.RowIndex];
            HandleSelectScheme(scheme);
        }

        private void DeleteSchemeButton_Click(object sender, EventArgs e)
        {
            if(SchemesDataGridView.SelectedRows.Count > 0)
            {
                var row = SchemesDataGridView.SelectedRows[0];
                int index = SchemesDataGridView.Rows.IndexOf(row);
                RenderScheme scheme = _parent.Schemes[index];
                _parent.Schemes.Remove(scheme);
                SchemesDataGridView.Rows.Remove(row);
            }
        }
    }
}
